/*
package Demo1;

import java.util.PriorityQueue;
import java.util.TimerTask;

class MyTimerTask implements Comparable<MyTimerTask>{
    private Runnable task;
    private long time;

    public MyTimerTask(Runnable task,long time){
        this.task=task;
        this.time=System.currentTimeMillis()+time;
    }

    public void run(){
        task.run();
    }

    public long getTime(){
        return time;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int)(this.time-o.time);
    }
}

class MyTimer{
    private PriorityQueue<MyTimerTask> queue=new PriorityQueue<>();
    Object blocker =new Object();
    private Thread thread=null;
    public MyTimer(){
        Thread t=new Thread(()->{
           while(true){
               synchronized (blocker){
                   try{
                       while(queue.isEmpty()){
                           blocker.wait();
                       }
                       MyTimerTask task=queue.peek();
                       if(System.currentTimeMillis()< task.getTime()){
                           blocker.wait(task.getTime()-System.currentTimeMillis());
                       }else{
                           task.run();
                           queue.poll();
                       }
                   }catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
        });
        t.start();
    }

    public void schedule(TimerTask task,long time){
        synchronized (blocker){
            MyTimerTask myTimerTask=new MyTimerTask(task,time);
            queue.offer(myTimerTask);
            blocker.notify();
        }
    }
}

public class Main2 {
    public static Thread thread=Thread.currentThread();
    public static void main(String[] args){
        MyTimer myTimer=new MyTimer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("1000");
            }
        },1000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("2000");
            }
        },2000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("3000");
            }
        },3000);
    }
}
*/
